var express = require("express");
var router = express.Router();
var blogInfoService = require("../../service/blog/blogInfoService");
var moment = require("moment");
const authMiddleware = require("../../middleware/authMiddleware");

//博客列表
router.get(
    "/list",
    async (req, res, next) => {
        let list = await blogInfoService.query(req.query);
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code:
             0, msg: err });
    }
);
//新增博客
router.post(
    "/add",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        blogInfoService
            .add({
                ...req.body,
                createTime: req.body.createTime || moment().format("YYYY-MM-DD hh:mm:ss"),
                updateTime: moment().format("YYYY-MM-DD hh:mm:ss"),
                readNum: 0,
            })
            .then(() => {
                res.json({ code: 1, msg: "新增成功" });
            });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//更新博客
router.post(
    "/update",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        blogInfoService
            .update({
                ...req.body,
                updateTime: moment().format("YYYY-MM-DD hh:mm:ss"),
            })
            .then(() => {
                res.json({ code: 1, msg: "更新成功" });
            });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//删除菜单
router.post(
    "/remove",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        let { _id } = req.body;
        if (_id == "" || !_id) return res.json({ code: 0, msg: "_id不存在" });
        blogInfoService.delete({ _id: _id }).then(() => {
            res.json({ code: 1, msg: "删除成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//查询
router.get(
    "/model",
    async (req, res, next) => {
        let { _id } = req.query;
        let list = await blogInfoService.query({ _id });
        res.json({ code: 1, data: list.length > 0 ? list[0] : {} });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//阅读博客
router.post(
    "/readBlog",
    async (req, res, next) => {
        let { _id } = req.body;
        let list = await blogInfoService.query({ _id })
        let readNum = 0
        if (list.length > 0) {
            if (!list[0].readNum) { readNum = 1 }
            else {
                readNum = list[0].readNum += 1
            }
        }
        blogInfoService
            .updateRedNum({ _id, readNum })
            .then(() => { res.json({ code: 1, msg: "更新成功" }) });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//获取博客类型+数量
router.get(
    "/getBlogTypeNum",
    async (req, res, next) => {
        let list = await blogInfoService.getBlogTypeNum();
        console.log('list', list)
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//获取博客标签+数量
router.get(
    "/getBlogTagNum",
    async (req, res, next) => {
        let list = await blogInfoService.getBlogTagNum();
        console.log('list', list)
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

module.exports = router;
